<?php

namespace app\command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;

class SystemCommand extends Command
{
    protected static $defaultName = 'system';
    protected static $defaultDescription = '执行系统命令';

    protected function configure()
    {
        $this->addArgument('cmd', InputArgument::REQUIRED, '要执行的系统命令');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $cmd = $input->getArgument('cmd');
        
        // 执行命令
        $result = [];
        $code = 0;
        exec($cmd . ' 2>&1', $result, $code);

        // 输出结果
        foreach ($result as $line) {
            $output->writeln($line);
        }

        // 根据命令执行的返回码决定命令是否成功
        return $code === 0 ? Command::SUCCESS : Command::FAILURE;
    }
} 